package com.ljx.ui;

import com.sun.tools.javac.Main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class UpdateJFrame extends JFrame implements ActionListener {

    //定义标题输入框
    JTextField titleText  = new JTextField();

    //定义内容的输入区域
    JTextArea contentText = new JTextArea();

    //定义修改按钮
    JButton update = new JButton("修改");

    //定义取消按钮
    JButton cancel = new JButton("取消");

    int i;
    public UpdateJFrame(int i) throws IOException
    {
        //i表示选中的文件序号
        this.i = i;

        initFrame();

        initView();

        this.setVisible(true);
    }

    private void initView() throws IOException {
        //定义最上面的每日一记
        JLabel title = new JLabel("每日一记");
        title.setBounds(240, 20, 584, 50);
        title.setFont(new Font("微软雅黑", Font.BOLD, 30));
        this.getContentPane().add(title);

        //定义文字：标题
        JLabel subject = new JLabel("标题");
        subject.setBounds(70,90,100,30);
        subject.setFont(new Font("微软雅黑",Font.PLAIN,16));
        this.getContentPane().add(subject);

        //定义文字：内容
        JLabel content = new JLabel("内容");
        content.setBounds(70,140,100,30);
        content.setFont(new Font("微软雅黑",Font.PLAIN,16));
        this.getContentPane().add(content);


        //设置标题的输入框
        titleText.setBounds(120,90,426,30);
        titleText.setFont(new Font("微软雅黑",Font.PLAIN,15));
        this.getContentPane().add(titleText);

        //设置内容的输入框
        contentText.setBounds(120,140,426,330);
        contentText.setFont(new Font("微软雅黑",Font.PLAIN,15));
        this.getContentPane().add(contentText);

        //将文件中的文本填入输入框
        getTextIn();

        //设置保存按钮
        update.setBounds(132,500,140,40);
        update.setFont(new Font("微软雅黑",Font.PLAIN,20));
        update.addActionListener(this);
        this.getContentPane().add(update);

        //设置取消按钮
        cancel.setBounds(312,500,140,40);
        cancel.setFont(new Font("微软雅黑",Font.PLAIN,20));
        cancel.addActionListener(this);
        this.getContentPane().add(cancel);
    }

    private void getTextIn() throws IOException {
        //更新框里的内容
        BufferedReader br = new BufferedReader(new FileReader("src/files/"+i+".txt"));
        //获取标题内容
        String tt = br.readLine();
        //获取正文内容
        String line;
        StringBuilder sb = new StringBuilder();
        while((line = br.readLine())!=null)
        {
            sb.append(line+"\n");
        }
        String ct = sb.toString();
        //关闭文件输入流
        br.close();
        //填入标题和正文输入框中
        titleText.setText(tt);
        contentText.setText(ct);
    }


    //对添加界面的一些设置
    private void initFrame() {
        //设置界面的宽高
        this.setSize(600, 600);
        //设置界面的标题
        this.setTitle("每日一记（修改日记）");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消默认的居中放置，只有取消了才会按照XY轴的形式添加组件
        this.setLayout(null);
        //设置背景颜色
        this.getContentPane().setBackground(new Color(212,212,212));
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == update)
        {
            System.out.println("点击了修改");
            //修改逻辑
            //将标题文本和内容文本再次填入文件输出流，写到文件中，不续写
            try {
                BufferedWriter bw = new BufferedWriter(new FileWriter("src/files/"+i+".txt"));
                //获取标题和内容文本
                String tt = titleText.getText();
                String ct = contentText.getText();
                //写入流
                bw.write(tt);
                bw.newLine();
                bw.write(ct);
                bw.newLine();
                //关闭流
                bw.close();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            //关闭当前界面
            this.setVisible(false);
            //开启主界面
            try {
                new MainJFrame();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
        else if(e.getSource() == cancel)
        {
            System.out.println("点击了取消");
            //取消逻辑，直接擦除文本内容，回到主界面即可
            titleText.setText("");
            contentText.setText("");
            this.setVisible(false);
            try {
                new MainJFrame();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
